package com.zxl.yjb.activity.home.sharemessage;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.district.DistrictItem;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zxl.yjb.R;
import com.zxl.yjb.adapter.AdListAdapter;
import com.zxl.yjb.adapter.MyFragmentPagerAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.AddrsBean;
import com.zxl.yjb.bean.AreaBean;
import com.zxl.yjb.bean.LocationBean;
import com.zxl.yjb.fragment.home.sharemessage.ShareMessage_Bendi_Fragment;
import com.zxl.yjb.fragment.home.sharemessage.ShareMessage_Bensheng_Fragment;
import com.zxl.yjb.fragment.home.sharemessage.ShareMessage_Benshi_Fragment;
import com.zxl.yjb.fragment.home.sharemessage.ShareMessage_Quxian_Fragment;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.TestAddressProvider;
import com.zxl.yjb.utils.ToastUtils;
import com.zxl.yjb.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import chihane.jdaddressselector.AddressSelector;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

public class MessageListActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    @BindView(R.id.iv_confirm2)
    ImageView iv_confirm2;
    @BindView(R.id.tv_tab)
    TabLayout tv_tab;
    @BindView(R.id.vp_list)
    NoScrollViewPager vp_list;
    @BindView(R.id.tv_select_location)
    TextView tvSelectLocation;
    @BindView(R.id.rl_select_location)
    RelativeLayout rlSelectLocation;

    private String name = "";
    private int id;

    public int REQUEST_POSITION = 11;

    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    private String[] title = {"本地", "区县", "本市", "本省"};
    private View[] tabView = new View[4];

    private ShareMessage_Bendi_Fragment shareMessage_bendi_fragment;
    private ShareMessage_Quxian_Fragment shareMessage_quxian_fragment;
    private ShareMessage_Benshi_Fragment shareMessage_benshi_fragment;
    private ShareMessage_Bensheng_Fragment shareMessage_bensheng_fragment;

    private List<AreaBean> provinceList = new ArrayList<>();
    private ObserverOnNextListener<List<AreaBean>> observerOnNextListener;
    private DataManager dataManager;

    private boolean isSelectedPosition;

    private String selectLocation = "";
    private String selectProvince = "";
    private String selectCity = "";
    private String selectDistrict = "";
    private String selectStreet = "";

    private LocationBean locationBean;
    private BottomDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_list);
        ButterKnife.bind(this);
        name = getIntent().getStringExtra("name");
        id = getIntent().getIntExtra("id", 0);
        locationBean = (LocationBean) SaveBeanUtil.getBean(MessageListActivity.this, Const.LOCATION_LNGLAT);
//        selectProvince = (String) SaveBeanUtil.getBean(this, Const.LOCATION_SELECT_PROVINCE);
//        selectCity = (String) SaveBeanUtil.getBean(this, Const.LOCATION_SELECT_CITY);
//        selectDistrict = (String) SaveBeanUtil.getBean(this, Const.LOCATION_SELECT_DISTRICT);
//        selectStreet = (String) SaveBeanUtil.getBean(this, Const.LOCATION_SELECT_STREET);
        selectLocation = selectProvince + " " + selectCity + " " + selectDistrict + " " + selectStreet;
        initView();
        if (TextUtils.isEmpty(selectLocation.trim())){
            getCurrentPositionAndLoadData();
            getLocation();
        }else {
            refreshShareMsgList();
        }

    }



    private void getLocation() {
        AMapLocationClient mlocationClient = new AMapLocationClient(this);
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        mlocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
                        if (isFinishing()) {
                            return;
                        }
                        //定位成功回调信息，设置相关消息
                        LocationBean locationBean = new LocationBean();
                        locationBean.setLat(amapLocation.getLatitude());
                        locationBean.setLng(amapLocation.getLongitude());
                        locationBean.setCountry(amapLocation.getCountry());
                        locationBean.setProvince(amapLocation.getProvince());
                        locationBean.setCity(amapLocation.getCity());
                        locationBean.setDistrict(amapLocation.getDistrict());
                        locationBean.setStreet(amapLocation.getStreet());
                        locationBean.setAddress(amapLocation.getAddress());
                        locationBean.setCitycode(amapLocation.getCityCode());
                        locationBean.setAdCode(amapLocation.getAdCode());
                        SaveBeanUtil.putBean(getApplicationContext(),Const.LOCATION_LNGLAT,locationBean);
                        getCurrentPositionAndLoadData();
                    }
                }
            }
        });
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setOnceLocation(true);
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setLocationCacheEnable(false);
        mlocationClient.setLocationOption(mLocationOption);
        mlocationClient.startLocation();
    }

    @Override
    protected void initView() {
        tv_page_title.setText(name);
        tv_confirm.setText("位置");
        tv_confirm.setTextColor(getResources().getColor(R.color.black_color));
        iv_confirm2.setImageResource(R.mipmap.zy_gxxx_wz);
        tv_confirm.setVisibility(View.VISIBLE);
        iv_confirm2.setVisibility(View.VISIBLE);


        tvSelectLocation.setText(selectLocation);
        if (TextUtils.isEmpty(selectLocation.trim())/*TextUtils.isEmpty(selectProvince)||TextUtils.isEmpty(selectCity)*//*||TextUtils.isEmpty(selectDistrict)||TextUtils.isEmpty(selectStreet)*/) {
            isSelectedPosition = false;
            rlSelectLocation.setVisibility(View.GONE);
        } else {
            isSelectedPosition = true;
            rlSelectLocation.setVisibility(View.VISIBLE);
        }


        shareMessage_bendi_fragment = new ShareMessage_Bendi_Fragment();
        shareMessage_quxian_fragment = new ShareMessage_Quxian_Fragment();
        shareMessage_benshi_fragment = new ShareMessage_Benshi_Fragment();
        shareMessage_bensheng_fragment = new ShareMessage_Bensheng_Fragment();
        fragmentList.add(shareMessage_bendi_fragment);
        fragmentList.add(shareMessage_quxian_fragment);
        fragmentList.add(shareMessage_benshi_fragment);
        fragmentList.add(shareMessage_bensheng_fragment);

        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        vp_list.setOffscreenPageLimit(3);
        vp_list.setAdapter(myFragmentPagerAdapter);

        tv_tab.setupWithViewPager(vp_list);
        vp_list.setCurrentItem(3, false);
        for (int i = 0; i < title.length; i++) {
            tabView[i] = LayoutInflater.from(MessageListActivity.this).inflate(R.layout.item_tab_order, null);
            ((TextView) tabView[i].findViewById(R.id.tv_tab_name)).setText(title[i]);
            tv_tab.getTabAt(i).setCustomView(tabView[i]);
        }

        tv_tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ((TextView) tabView[tab.getPosition()].findViewById(R.id.tv_tab_name)).setTextColor(getResources().getColor(R.color.main_color));
//                tabView[tab.getPosition()].findViewById(R.id.view_line).setVisibility(View.VISIBLE);
                vp_list.setCurrentItem(tab.getPosition(), false);

                if (tab.getPosition() != 3 && !isSelectedPosition) {
                    ToastUtils.show(MessageListActivity.this, "请您先通过右上角的位置进行精确查询");
                    return;
                }
                if (tab.getPosition()==0&& TextUtils.isEmpty(selectStreet)) {
                    showSelecteStreetPP();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ((TextView) tabView[tab.getPosition()].findViewById(R.id.tv_tab_name)).setTextColor(getResources().getColor(R.color.black_color));
                tabView[tab.getPosition()].findViewById(R.id.view_line).setVisibility(View.GONE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

//        ((TextView) tabView[0].findViewById(R.id.tv_tab_name)).setTextColor(getResources().getColor(R.color.main_color));
//        tabView[0].findViewById(R.id.view_line).setVisibility(View.VISIBLE);


    }

    private void showSelecteStreetPP() {
        if (dialog != null) {
            dialog.show();
            return;
        }
        String adCode = locationBean.getAdCode();
        final List<AreaBean> adList = new ArrayList<>();
        dataManager = new DataManager();
        ObserverOnNextListener<AddrsBean> listener = new ObserverOnNextListener<AddrsBean>() {
            @Override
            public void onNext(AddrsBean addrsBean) {
                if (addrsBean.getStatus().equals("1")) {
                    List<AddrsBean.DistrictsBeanX> districts = addrsBean.getDistricts();
                    if (districts != null && districts.size() > 0) {
                        AddrsBean.DistrictsBeanX districtsBeanX = districts.get(0);
                        if (districtsBeanX != null && districts.size() > 0) {
                            List<AddrsBean.DistrictsBeanX.DistrictsBean> districts1 = districtsBeanX.getDistricts();
                            for (AddrsBean.DistrictsBeanX.DistrictsBean districtsBean : districts1) {
                                adList.add(new AreaBean(districtsBean.getName(),districtsBean.getAdcode()));
                            }

                        }

                    }

                }

                View view = View.inflate(MessageListActivity.this, R.layout.ad_list, null);
                final Dialog dialog = DisplayUtil.showDialog(MessageListActivity.this, view, Gravity.BOTTOM);
                RecyclerView recyclerView = view.findViewById(R.id.adlist);
                AdListAdapter adapter = new AdListAdapter(R.layout.ad_list_item, adList);
                LinearLayoutManager layoutManager = new LinearLayoutManager(MessageListActivity.this);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(adapter);
                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        AreaBean item = (AreaBean) adapter.getItem(position);
                        selectStreet= item.getName();
                        String s =selectProvince + " " + selectCity + " " + selectDistrict + " "+selectStreet ;
                        tvSelectLocation.setText(s);
                        dialog.dismiss();
                        if (shareMessage_bendi_fragment != null) {
                            if (TextUtils.isEmpty(locationBean.getStreet())) {
                                shareMessage_bendi_fragment.refreshShareMsgList("", "", "", "");
                            } else {
                                shareMessage_bendi_fragment.refreshShareMsgList(selectProvince, selectCity,selectDistrict, selectStreet);

                            }
                        }
                    }
                });

            }
        };
        dataManager.getAllProvince(new ProgressObserver<AddrsBean>(listener, this) {
            @Override
            public void showDialog() {
            }
        }, "https://restapi.amap.com/v3/config/district?keywords="+adCode+"&subdistrict=1&key=b26ad2beac5a6030f696d4d68e077a7b");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_POSITION) {
            PoiItem poiItem = data.getParcelableExtra("position");
            if (poiItem != null) {
                /*if (shareMessage_bendi_fragment!=null)
                {
                    shareMessage_bendi_fragment.refreshShareMsgList(poiItem);
                }

                if (shareMessage_quxian_fragment!=null)
                {
                    shareMessage_quxian_fragment.refreshShareMsgList(poiItem);
                }

                if (shareMessage_benshi_fragment!=null)
                {
                    shareMessage_benshi_fragment.refreshShareMsgList(poiItem);
                }

                if (shareMessage_bensheng_fragment!=null)
                {
                    shareMessage_bensheng_fragment.refreshShareMsgList(poiItem);
                }*/
            }

        }
    }


    private void getCurrentPositionAndLoadData() {
        if (locationBean != null) {
            selectProvince = locationBean.getProvince();
            selectCity = locationBean.getCity();
            if (selectCity.equals(selectProvince)) {
                selectCity = selectCity.replace("市", "城区");
            }
            selectDistrict = locationBean.getDistrict();

            String s =selectProvince + " " + selectCity + " " + selectDistrict + " " ;

            isSelectedPosition = true;
            rlSelectLocation.setVisibility(View.VISIBLE);
            tvSelectLocation.setText(s);

           /* if (shareMessage_bendi_fragment != null) {
                if (TextUtils.isEmpty(locationBean.getStreet())) {
                    shareMessage_bendi_fragment.refreshShareMsgList("", "", "", "");

                } else {
                    shareMessage_bendi_fragment.refreshShareMsgList(locationBean.getProvince(), locationBean.getCity(), locationBean.getDistrict(), locationBean.getStreet());

                }
            }*/

            if (shareMessage_quxian_fragment != null) {
                if (TextUtils.isEmpty(locationBean.getDistrict())) {
                    shareMessage_quxian_fragment.refreshShareMsgList("", "", "", "");
                } else {
                    vp_list.setCurrentItem(1);
                    shareMessage_quxian_fragment.refreshShareMsgList(selectProvince, selectCity, selectDistrict, "");
                }
            }

            if (shareMessage_benshi_fragment != null) {
                if (TextUtils.isEmpty(locationBean.getCity())) {
                    shareMessage_benshi_fragment.refreshShareMsgList("","", "", "");
                } else {
                    shareMessage_benshi_fragment.refreshShareMsgList(selectProvince, selectCity,"", "");
                }
            }

            if (shareMessage_bensheng_fragment != null) {
                shareMessage_bensheng_fragment.refreshShareMsgList(selectProvince, "", "", "");
            }
        }
    }


    /**
     * 设置地址
     *
     * @param provinceList
     */
    private void setAddress(List<AreaBean> provinceList) {
        selectProvince = "";
        selectCity = "";
        selectDistrict = "";
        selectStreet = "";

        final TextView locationText = new TextView(this);
        locationText.setVisibility(View.GONE);
        AddressSelector selector = new AddressSelector(this);
        selector.setAddressProvider(new TestAddressProvider(provinceList, MessageListActivity.this, new TestAddressProvider.PositionSelectListener() {
            @Override
            public void onProvinceSelected(DistrictItem province) {
                selectProvince = province.getName();
                if (locationText != null) {
                    locationText.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onCitySelected(DistrictItem city) {
                selectCity = city.getName();
            }

            @Override
            public void onCountySelected(DistrictItem county) {
                selectDistrict = county.getName();
            }

            @Override
            public void onStreetSelected(DistrictItem street) {
//                selectStreet = street
            }
        }));

        dialog = new BottomDialog(this);
        selector.setOnAddressSelectedListener(new OnAddressSelectedListener() {
            @Override
            public void onAddressSelected(Province province, City city, County county, Street street) {
                String s =
                        (province == null ? "" : province.name) +
                                (city == null ? "" : " " + city.name) +
                                (county == null ? "" : " " + county.name) +
                                (street == null ? "" : " " + street.name);
                if (street != null) {
                    selectStreet = street.name;
                    SaveBeanUtil.putBean(MessageListActivity.this, Const.LOCATION_SELECT_PROVINCE, province.name);
                    SaveBeanUtil.putBean(MessageListActivity.this, Const.LOCATION_SELECT_CITY, city.name);
                    SaveBeanUtil.putBean(MessageListActivity.this, Const.LOCATION_SELECT_DISTRICT, county.name);
                    SaveBeanUtil.putBean(MessageListActivity.this, Const.LOCATION_SELECT_STREET, street.name);
                    rlSelectLocation.setVisibility(View.VISIBLE);
                    tvSelectLocation.setText(s);

                    isSelectedPosition = true;
                    if (shareMessage_bendi_fragment != null) {
                        shareMessage_bendi_fragment.refreshShareMsgList(province.name, city.name, county.name, street.name);
                    }

                    if (shareMessage_quxian_fragment != null) {
//                        shareMessage_quxian_fragment.refreshShareMsgList(province.name, city.name, county.name, street.name);
                        shareMessage_quxian_fragment.refreshShareMsgList(province.name, city.name, county.name, "");
                    }

                    if (shareMessage_benshi_fragment != null) {
//                        shareMessage_benshi_fragment.refreshShareMsgList(province.name, city.name, county.name, street.name);
                        shareMessage_benshi_fragment.refreshShareMsgList(province.name, city.name, "", "");
                    }

                    if (shareMessage_bensheng_fragment != null) {
//                        shareMessage_bensheng_fragment.refreshShareMsgList(province.name, city.name, county.name, street.name);
                        shareMessage_bensheng_fragment.refreshShareMsgList(province.name, "", "", "");
                    }
                    vp_list.setCurrentItem(0);
                }
                dialog.cancel();
            }
        });
        ViewGroup.LayoutParams layout = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        ViewGroup selectorView = (ViewGroup) selector.getView();
        ViewGroup tabView = (ViewGroup) selectorView.getChildAt(0);

        locationText.setText("查看当前区域");
        locationText.setTextColor(getResources().getColor(R.color.white_color));
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        layoutParams.setMargins(0, 0, DisplayUtil.dip2px(this, 5), 0);
        locationText.setLayoutParams(layoutParams);
        locationText.setPadding(DisplayUtil.dip2px(this, 5), DisplayUtil.dip2px(this, 5), DisplayUtil.dip2px(this, 5), DisplayUtil.dip2px(this, 5));
        locationText.setBackgroundResource(R.drawable.btn_submit);
        locationText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /*LocationBean locationBean = (LocationBean) SaveBeanUtil.getBean(MessageListActivity.this,Const.LOCATION_LNGLAT);
                if (locationBean == null)
                {
                    ToastUtils.show(MessageListActivity.this,"当前暂无定位，定打开应用的定位权限后重启APP再尝试");
                    dialog.cancel();
                    return;
                }

                String s = locationBean.getProvince()+" "+locationBean.getCity()+" "+locationBean.getDistrict()+" "+locationBean.getStreet();*/

                if (!TextUtils.isEmpty(selectProvince)
                        & !TextUtils.isEmpty(selectCity)
                        & !TextUtils.isEmpty(selectDistrict)
                        & !TextUtils.isEmpty(selectStreet)) {
                    vp_list.setCurrentItem(0);
                } else if (!TextUtils.isEmpty(selectProvince)
                        & !TextUtils.isEmpty(selectCity)
                        & !TextUtils.isEmpty(selectDistrict)
                        & TextUtils.isEmpty(selectStreet)) {
                    vp_list.setCurrentItem(1);
                } else if (!TextUtils.isEmpty(selectProvince)
                        & !TextUtils.isEmpty(selectCity)
                        & TextUtils.isEmpty(selectDistrict)
                        & TextUtils.isEmpty(selectStreet)) {
                    vp_list.setCurrentItem(2);
                } else if (!TextUtils.isEmpty(selectProvince)
                        & TextUtils.isEmpty(selectCity)
                        & TextUtils.isEmpty(selectDistrict)
                        & TextUtils.isEmpty(selectStreet)) {
                    vp_list.setCurrentItem(3);
                }

                SaveBeanUtil.putBean(MessageListActivity.this, Const.LOCATION_SELECT_PROVINCE, selectProvince);
                SaveBeanUtil.putBean(MessageListActivity.this, Const.LOCATION_SELECT_CITY, selectCity);
                SaveBeanUtil.putBean(MessageListActivity.this, Const.LOCATION_SELECT_DISTRICT, selectDistrict);
                SaveBeanUtil.putBean(MessageListActivity.this, Const.LOCATION_SELECT_STREET, selectStreet);

                rlSelectLocation.setVisibility(View.VISIBLE);
                tvSelectLocation.setText(selectProvince + " " + selectCity + " " + selectDistrict + " " + selectStreet);
                refreshShareMsgList();
               /* if (shareMessage_bendi_fragment != null) {
                    if (TextUtils.isEmpty(selectStreet)) {
                        shareMessage_bendi_fragment.refreshShareMsgList("", "", "", "");
                    } else {
                        shareMessage_bendi_fragment.refreshShareMsgList(selectProvince, selectCity, selectDistrict, selectStreet);
                    }

                }

                if (shareMessage_quxian_fragment != null) {
                    if (TextUtils.isEmpty(selectDistrict)) {
                        shareMessage_quxian_fragment.refreshShareMsgList("", "", "", "");
                    } else {
                        shareMessage_quxian_fragment.refreshShareMsgList(selectProvince, selectCity, selectDistrict, selectStreet);
                    }

                }

                if (shareMessage_benshi_fragment != null) {
                    if (TextUtils.isEmpty(selectCity)) {
                        shareMessage_benshi_fragment.refreshShareMsgList("", "", "", "");
                    } else {
                        shareMessage_benshi_fragment.refreshShareMsgList(selectProvince, selectCity, selectDistrict, selectStreet);
                    }

                }

                if (shareMessage_bensheng_fragment != null) {
                    shareMessage_bensheng_fragment.refreshShareMsgList(selectProvince, selectCity, selectDistrict, selectStreet);
                }*/
                dialog.cancel();
            }
        });
        tabView.addView(locationText);

        dialog.addContentView(selector.getView(), layout);
        dialog.show();
    }


    private void refreshShareMsgList() {
        if (shareMessage_bendi_fragment != null) {
            if (TextUtils.isEmpty(selectStreet)) {
                shareMessage_bendi_fragment.refreshShareMsgList("", "", "", "");
            } else {
                shareMessage_bendi_fragment.refreshShareMsgList(selectProvince, selectCity, selectDistrict, selectStreet);
            }

        }

        if (shareMessage_quxian_fragment != null) {
            if (TextUtils.isEmpty(selectDistrict)) {
                shareMessage_quxian_fragment.refreshShareMsgList("", "", "", "");
            } else {
                shareMessage_quxian_fragment.refreshShareMsgList(selectProvince, selectCity, selectDistrict, "");
            }

        }

        if (shareMessage_benshi_fragment != null) {
            if (TextUtils.isEmpty(selectCity)) {
                shareMessage_benshi_fragment.refreshShareMsgList("", "", "", "");
            } else {
                shareMessage_benshi_fragment.refreshShareMsgList(selectProvince, selectCity, "", "");
            }

        }

        if (shareMessage_bensheng_fragment != null) {
            shareMessage_bensheng_fragment.refreshShareMsgList(selectProvince, "", "", "");
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_confirm, R.id.iv_confirm2, R.id.iv_clear})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_confirm:
                /*Intent intent = new Intent(MessageListActivity.this, PositionActivity.class);
                intent.putExtra("origin","MessageListActivity");
                startActivityForResult(intent,REQUEST_POSITION);*/
                if (provinceList != null && provinceList.size() != 0) {
                    setAddress(provinceList);
                } else {

                    getAllProvince();
                }
                    /*dataManager = new DataManager();
                    observerOnNextListener = new ObserverOnNextListener<List<AreaBean>>() {
                        @Override
                        public void onNext(List<AreaBean> provinceBeanList) {
                            provinceList = provinceBeanList;
                            setAddress(provinceList);
                        }
                    };
                    dataManager.getAreaAllProvince(new ProgressObserver<List<AreaBean>>(observerOnNextListener, this), new HashMap<String, String>());*/
//                    https://restapi.amap.com/v3/config/district?keywords=%E9%82%AF%E9%83%B8%E5%B8%82&subdistrict=3&key=b26ad2beac5a6030f696d4d68e077a7b

                    /*GetPositionUtil.getPosition(MessageListActivity.this, "", new GetPositionUtil.SearchResult() {
                        @Override
                        public void onResult(DistrictResult districtResult) {

                        }

                        @Override
                        public void onListResult(List<AreaBean> list) {
                            provinceList = list;
                            setAddress(provinceList);
                        }
                    });

                }*/
                break;
            case R.id.iv_confirm2:
                /*Intent intent2 = new Intent(MessageListActivity.this, PositionActivity.class);
                intent2.putExtra("origin","MessageListActivity");
                startActivityForResult(intent2,REQUEST_POSITION);*/

                if (provinceList != null && provinceList.size() != 0) {
                    setAddress(provinceList);
                } else {
                    getAllProvince();
                    /*dataManager = new DataManager();
                    observerOnNextListener = new ObserverOnNextListener<List<AreaBean>>() {
                        @Override
                        public void onNext(List<AreaBean> provinceBeanList) {
                            provinceList = provinceBeanList;
                            setAddress(provinceList);
                        }
                    };
                    dataManager.getAreaAllProvince(new ProgressObserver<List<AreaBean>>(observerOnNextListener, this), new HashMap<String, String>());*/
                   /* GetPositionUtil.getPosition(MessageListActivity.this, "", new GetPositionUtil.SearchResult() {
                        @Override
                        public void onResult(DistrictResult districtResult) {

                        }

                        @Override
                        public void onListResult(List<AreaBean> list) {
                            provinceList = list;
                            setAddress(provinceList);
                        }
                    });*/

                }
                break;
            case R.id.iv_clear:
                rlSelectLocation.setVisibility(View.GONE);
                break;

            default:
                break;
        }
    }

    private void getAllProvince() {
        dataManager = new DataManager();
        ObserverOnNextListener<AddrsBean> listener = new ObserverOnNextListener<AddrsBean>() {
            @Override
            public void onNext(AddrsBean addrsBean) {
                if (addrsBean.getStatus().equals("1")) {
                    List<AddrsBean.DistrictsBeanX> districts = addrsBean.getDistricts();
                    if (districts != null && districts.size() > 0) {
                        AddrsBean.DistrictsBeanX districtsBeanX = districts.get(0);
                        if (districtsBeanX != null && districts.size() > 0) {
                            List<AddrsBean.DistrictsBeanX.DistrictsBean> districts1 = districtsBeanX.getDistricts();
                            for (AddrsBean.DistrictsBeanX.DistrictsBean districtsBean : districts1) {
                                provinceList.add(new AreaBean(districtsBean.getName(),districtsBean.getAdcode()));
                            }

                        }

                    }

                }

                setAddress(provinceList);
            }
        };
        dataManager.getAllProvince(new ProgressObserver<AddrsBean>(listener, this) {
            @Override
            public void showDialog() {
            }
        }, "https://restapi.amap.com/v3/config/district?keywords=中国&subdistrict=1&key=b26ad2beac5a6030f696d4d68e077a7b");
    }
}
